package com.klhappy.houyuan.activity;

import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.google.gson.Gson;
import com.klhappy.houyuan.bean.AddressBean.DeAddressBean;
import com.klhappy.houyuan.bean.AddressBean.DefaultAddress;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.utils.NotifyMethod;
import com.klhappy.houyuan.view.swipemenulistview.SwipeMenu;
import com.klhappy.houyuan.view.swipemenulistview.SwipeMenuCreator;
import com.klhappy.houyuan.view.swipemenulistview.SwipeMenuItem;
import com.klhappy.houyuan.view.swipemenulistview.SwipeMenuListView;
import com.klhappy.houyuan.view.AbsActivity;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.adapter.AddAddressAdapter;
import com.klhappy.houyuan.bean.AddressBean.AddressBean;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.JiaYuanModle;
import com.klhappy.houyuan.utils.JsonCode;

public class Add_AddressActivity extends AbsActivity implements CallBackInterModle {
	public static final String AVG = "Add_AddressActivity";
	@InjectView(R.id.lv_receving_address)
	SwipeMenuListView receving_address_list;
	@InjectView(R.id.title)
	TextView title;

	@OnClick(R.id.click_ra)
	public void click_ra(){
		intent.putExtra("addressx", address);
		intent.putExtra("id" ,id);
		setResult(50, intent);
		finish();
	}
	@OnClick(R.id.add_loction)
	public void add_loction(){
		Intent intent = new Intent(this , AddLocationActivity.class);
		startActivity(intent);
	}

	private JiaYuanModle modle;
	private AddressBean addressBean;
	private AddressBean.Records records;
	private List<AddressBean.Records> lists;
	private AddAddressAdapter adapter;
	private String address;
	private String id;
	private Intent intent;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_receving_address);
		ButterKnife.inject(this);
		title.setText("收货地址");
		modle = new JiaYuanModle(this);
		modle.setCallBackInterModle(this);
		showDialog("请稍等...");

		intent = new Intent();
		setSwipeMenu();
		NotifyMethod.getInstance().addObserver(AVG, new NotifyMethod.NotifyListener() {
			@Override
			public void notify(Object o) {
				records = (AddressBean.Records) o;
				id = records.getId() + "";
				address = records.getAddress();
				showDialog("设置中...");
				modle.setNewAddress(records);
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		modle.getAddress();
	}

	@Override
	public void CallBackFromModle(String url, BaseJsonBean o) {
		if (url.equals(UrlConstant.ADRESS_URL)){
			if (o.getCode() == 200){
				addressBean = JsonCode.getClass(o.getEntity(), AddressBean.class);
				lists = JsonCode.getClassList(addressBean.getRecords(), AddressBean.Records.class);
				try {
					if (lists != null){
						adapter = new AddAddressAdapter(this, lists);
						receving_address_list.setAdapter(adapter);
						address = lists.get(0).getAddress();
					}
				}catch (Exception e){

				}

			}
			closeDialog();
		}else if (url.equals(UrlConstant.REMOVEADDRESS_URL)){
			if (o.getCode() == 200){
				showToast(o.getMsg());
			}
			closeDialog();
		}else if (url.equals(UrlConstant.ACCOUNT_URL)){
			if (o.getCode() == 200){
				mConfigManage.remove(KeyConstant.ADDRESS);
				showToastx(o.getMsg());
				mConfigManage.set(KeyConstant.ADDRESS, id);
			}
			closeDialog();
		}
	}

	private void setSwipeMenu(){
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				SwipeMenuItem openItem = new SwipeMenuItem(Add_AddressActivity.this);
				openItem.setBackground(new ColorDrawable(Color.rgb(0x86, 0x9e, 0x34)));
				openItem.setWidth(dp2px(68));
				openItem.setTitle("删除");
				openItem.setTitleSize(18);
				openItem.setTitleColor(Color.WHITE);
				menu.addMenuItem(openItem);
			}
		};
		receving_address_list.setMenuCreator(creator);

		receving_address_list.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {
				switch (index) {
					case 0:
						modle.RemoveAddress(lists.get(position).getId() + "");
						lists.remove(position);
						adapter.notifyDataSetChanged();
						showDialog("请稍等...");
						break;
					default:
						break;
				}
			}
		});

		receving_address_list.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {

			@Override
			public void onSwipeStart(int position) {
				// swipe start
			}

			@Override
			public void onSwipeEnd(int position) {
				// swipe end
			}
		});
	}

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getRepeatCount() == 0) {
			intent.putExtra("addressx", address);
			intent.putExtra("id" ,id);
			setResult(50, intent);
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
